package com.woniu.his.sms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.his.exception.HisException;
import com.woniu.his.sms.common.SmsResultCode;
import com.woniu.his.sms.common.enums.SmsStatusEnums;
import com.woniu.his.sms.infra.model.Dept;
import com.woniu.his.sms.infra.mapper.DeptMapper;
import com.woniu.his.sms.infra.model.DeptType;
import com.woniu.his.sms.service.DeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.his.sms.service.DeptTypeService;
import com.woniu.his.sms.service.dto.DeptDto;
import com.woniu.his.sms.service.dto.PageDeptDto;
import com.woniu.his.sms.service.param.AddDeptServiceParam;
import com.woniu.his.sms.service.param.FindDeptServiceParam;
import com.woniu.his.sms.service.param.UpdateDeptServiceParam;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 科室表 服务实现类
 * </p>
 *
 * @author cb
 * @since 2022-11-25
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {

    @Resource
    private DeptTypeService deptTypeService;

    @Override
    public void addDept(AddDeptServiceParam addDeptParam) {

        //查找是否有重名的科室编码
        QueryWrapper<Dept> wrapper1=new QueryWrapper<>();
        wrapper1.eq("dept_code",addDeptParam.getDeptCode());
        Dept one1 = getOne(wrapper1);
        //查找是否有重名的科室名
        QueryWrapper<Dept> wrapper2=new QueryWrapper<>();
        wrapper2.eq("dept_name",addDeptParam.getDeptName());
        Dept one2 = getOne(wrapper2);

        if (one1!=null) throw new HisException(SmsResultCode.DEPT_CODE_IS_EXIST);
        if (one2!=null) throw new HisException(SmsResultCode.DEPT_NAME_IS_EXIST);

        //判断完后，新增操作
        DeptType deptType = deptTypeService.getById(addDeptParam.getDeptTypeId());
        if (deptType==null) throw new HisException(SmsResultCode.DEPT_TYPE_NOT_EXIST);
        Dept dept=new Dept();
        dept.setDeptCode(addDeptParam.getDeptCode());
        dept.setDeptName(addDeptParam.getDeptName());
        dept.setDeptTypeId(addDeptParam.getDeptTypeId());
        dept.setDeptStatus(SmsStatusEnums.ON.getCode());
        dept.setDeptDescription(addDeptParam.getDeptDescription());
        save(dept);

    }

    @Override
    public PageDeptDto findDept(FindDeptServiceParam serviceParam) {

        Long current=serviceParam.getCurrent()==null ? 1 : serviceParam.getCurrent();
        Long size=serviceParam.getSize()==null ? 1 : serviceParam.getSize();

        QueryWrapper<Dept> wrapper=new QueryWrapper<>();
        //条件0：选择有效的
        wrapper.eq("dept_status",SmsStatusEnums.ON.getCode());
        //条件1：判断科室类别ID是否传入
        if (serviceParam.getDeptTypeId()!=null&&serviceParam.getDeptTypeId()>=1){
            wrapper.eq("dept_type_id",serviceParam.getDeptTypeId());
        }
        //条件2：关键字（科室名，科室编码）是否存在
        if (serviceParam.getKeyword()!=null&&serviceParam.getKeyword().trim().length()>0){
            wrapper.and(w->{
                w.like("dept_code",serviceParam.getKeyword()).or().like("dept_name",serviceParam.getKeyword());
            });
        }
        //最终查询数据
        Page<Dept> page = page(new Page<>(current,size), wrapper);
        if(page.getRecords().size()<=0) return null;
        PageDeptDto pageDeptDto=new PageDeptDto();
        List<DeptDto> list= BeanUtil.copyToList(page.getRecords(),DeptDto.class);
        //查询类别,插入科室类别名
        for (DeptDto deptDto : list) {
            deptDto.setDeptTypeName(deptTypeService.getById(deptDto.getDeptTypeId()).getDeptTypeName());
        }

        pageDeptDto.setCurrent(page.getCurrent());
        pageDeptDto.setSize(page.getSize());
        pageDeptDto.setTotal(page.getTotal());
        pageDeptDto.setDeptDtoList(list);

        return pageDeptDto;
    }

    @Override
    public List<DeptDto> FindAllDept() {

        QueryWrapper<Dept> wrapper=new QueryWrapper<>();
        //条件0：选择有效的
        wrapper.eq("dept_status",SmsStatusEnums.ON.getCode());
        List<DeptDto> list = BeanUtil.copyToList(list(wrapper),DeptDto.class);
        for (DeptDto deptDto : list) {
            deptDto.setDeptTypeName(deptTypeService.getById(deptDto.getDeptTypeId()).getDeptTypeName());
        }
        return list;
    }

    @Override
    public void deleteDeptByIds(List<Integer> ids) {

        removeByIds(ids);
    }

    @Override
    public void updateDeptById(UpdateDeptServiceParam updateDeptServiceParam) {

        Dept dept = getById(updateDeptServiceParam.getId());
        if (dept==null) throw new HisException(SmsResultCode.DEPT_NOT_EXIST);

        //更改时，查找是否有重名的科室编码
        QueryWrapper<Dept> wrapper1=new QueryWrapper<>();
        wrapper1.eq("dept_code",updateDeptServiceParam.getDeptCode());
        Dept one1 = getOne(wrapper1);
        //更改时，查找是否有重名的科室名
        QueryWrapper<Dept> wrapper2=new QueryWrapper<>();
        wrapper2.eq("dept_name",updateDeptServiceParam.getDeptName());
        Dept one2 = getOne(wrapper2);

        if (one1!=null) throw new HisException(SmsResultCode.DEPT_CODE_IS_EXIST);
        if (one2!=null) throw new HisException(SmsResultCode.DEPT_NAME_IS_EXIST);

        dept.setDeptCode(updateDeptServiceParam.getDeptCode());
        dept.setDeptName(updateDeptServiceParam.getDeptName());
        if (updateDeptServiceParam.getDeptDescription()!=null){
            dept.setDeptDescription(updateDeptServiceParam.getDeptDescription());
        }
        updateById(dept);
    }

    @Override
    public DeptDto findDeptById(Integer id) {

        if (id==null||id<=0) throw new HisException(SmsResultCode.ID_IS_WRONG);
        Dept one = getById(id);
        DeptDto result =null;
        if (one!=null) {
            result = BeanUtil.copyProperties(one, DeptDto.class);
            result.setDeptTypeName(deptTypeService.getById(result.getDeptTypeId()).getDeptTypeName());
        }
        return result;
    }
}
